package com.ssm.controller;

import com.alibaba.fastjson.JSONObject;
import com.ssm.pojo.Department;
import com.ssm.service.DepartmentService;
import com.ssm.util.TreeList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 根据实体类生成属性表格菜单 实体类有两个必须属性（set get方法） 会自动按照（id/pid）顺序递归对list集合内容排序 String
 * depart_id;//唯一id String parentid;//父id
 *
 * @author Administrator
 */
@Controller
public class TreeListController {
    @Autowired
    private DepartmentService departmentService;

//    @ResponseBody
//    @RequestMapping(value="/user/allList",method={RequestMethod.GET})
//    public List<Department> queryList() {
//        return departmentService.queryList();
//    }

    @ResponseBody
    @RequestMapping(value="/user/allList",method={RequestMethod.POST})
    public String queryList() {

        Map<String, Object> map = new HashMap<String, Object>();
        List<Department> list = departmentService.queryList();
        TreeList tree = new TreeList(list);
        List<Department> listTree = tree.buildTree();
        map.put("rows", listTree);
        map.put("total", listTree.size());
        String str = JSONObject.toJSONString(map);

        return str;
    }
    @ResponseBody
    @RequestMapping(value="/user/allListEl",method={RequestMethod.GET})
    public String queryListEl() {

        Map<String, Object> map = new HashMap<String, Object>();

        List<Department> list = departmentService.queryList();
        TreeList tree = new TreeList(list);
        List<Department> listTree = tree.buildTree();
        map.put("rows", listTree);
        map.put("total", listTree.size());
        String str = JSONObject.toJSONString(map);

        return str;
    }



}
